import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2022-11-29
 * Time:20:52
 */
/*设置状态码*/
@WebServlet("/status")
public class StatusServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //约定,浏览器 query string 传个参数过来
        //形如 type = 1
        //如果type为1,返回200;type为2,返回404;type为3返回一个500;
        String type = req.getParameter("type");
        if (type.equals("1")){
            resp.setStatus(200);
        }else if (type.equals("2")){
            resp.setStatus(404);
            resp.sendError(404);
        }else if (type.equals("3")){
            resp.setStatus(500);
        }else {
            resp.setStatus(504);
        }
    }
}
